<template>
  <!-- 周转卡表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="65%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100%">
      <el-card shadow="never" class="table-container">
        <div class="formTable2">
          <el-table
            height="100%"
            :data="formData.data"
            :key="Math.random"
            border
            style="width: 100%"
          >
            <el-table-column
              type="index"
              label="序号"
              align="center"
              width="70"
            />
            <el-table-column
              label="周转卡编号"
              prop="turnoverCode"
              min-width="100"
              show-overflow-tooltip
            />
            <el-table-column
              label="产品名称"
              prop="materialCode"
              min-width="180"
              show-overflow-tooltip
            >
              <template #default="{ row }">
                <span v-if="row.materialCode"
                  >{{ row.materialName }}
                </span>
                <span v-else></span>
              </template>
            </el-table-column>
            <el-table-column
              label="图号"
              prop="figureNumber"
              min-width="100"
              show-overflow-tooltip
            />
            <el-table-column
              label="批次号"
              prop="materialBatch"
              min-width="100"
              show-overflow-tooltip
            />
            <el-table-column
              label="数量"
              prop="materialQuantity"
              min-width="100"
              show-overflow-tooltip
              header-align="center"
              align="right"
            >
              <template #default="{ row }">
                {{ row.materialQuantity || "" }}
              </template>
            </el-table-column>
            <el-table-column
              label="工序"
              prop="processesClassification"
              min-width="100"
              show-overflow-tooltip
            >
              <template #default="{ row }">
                {{ useDictionary("ProcessType", row.processesClassification) }}
              </template>
            </el-table-column>
            <el-table-column
              label="周转卡类型"
              prop="turnoverType"
              min-width="100"
              show-overflow-tooltip
            >
              <template #default="{ row }">
                {{ useDictionary("TurnoverType", row.turnoverType) }}
              </template>
            </el-table-column>
            <el-table-column
              label="数量单位"
              prop="quantityUnit"
              min-width="100"
              show-overflow-tooltip
            >
              <template #default="{ row }">
                {{ useDictionary("MeasurementUnit", row.quantityUnit) }}
              </template>
            </el-table-column>
            <el-table-column
              label="创建时间"
              prop="createTime"
              min-width="160"
              show-overflow-tooltip
              align="center"
            />
            <el-table-column
              label="创建人"
              prop="createdUserName"
              min-width="100"
              show-overflow-tooltip
            />
            <el-table-column
              label="更新时间"
              prop="lastUpdateTime"
              min-width="160"
              show-overflow-tooltip
              align="center"
            />
            <el-table-column
              label="更新人"
              prop="lastUpdatedUserName"
              min-width="100"
              show-overflow-tooltip
            />
          </el-table>
        </div>
      </el-card>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary";
import { useDictionary } from "@/utils/useTable"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESTURNOVERCARDDETAILEDIT",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  organizationId: [
    { length: 2, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  organizationCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  plantId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  plantCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  turnoverCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  figureNumber: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workingFormId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  status: [],
  workCenterId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  workCenterCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workCenterName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  produceStorageId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  processesId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  processesClassification: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialQuantity: [],
  quantityUnit: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  turnoverType: [],
  materialBatch: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialBarcode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  version: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  deleted: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  data: [],
});

watch(
  () => props.data,
  (newVal: any) => {
    const { data } = newVal;
    Object.assign(formData, {
      data,
    });
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.data = [];
}

const loading = ref(false);

/** 周转卡表保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesturnovercard/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
